app.controller("z-retroactive-c", [ '$scope','$rootScope','$z-http','$sce','z-retroactive-s','z-attendance-s','z-edit-menu-s','z-role-s','$filter',
		function($scope,$rootScope,$zhttp,$sce,Service,AttendanceService,MenuService,RoleService,$filter) {
		$scope.viewModel = {};
		$scope.viewModel.approvalRecordBean="";
		$scope.viewModel.perPage = "10";
		$scope.viewModel.rowCount = "1";		  
		$scope.viewModel.attendanceState=""       //考勤状态
		$scope.viewModel.retroactiveNumber=""     //补签次数
		var now = new Date();                     //当前日期     
		var nowMonth = now.getMonth();            //当前月     
		var nowYear=$filter('date')(now,'yyyy');  //当前年
		//获取当月开始日期
		var monthStartDate = $filter("date")(new Date(nowYear, nowMonth, 1), "yyyy/MM/dd");
		//获取当前日期
        var dayEndDate= $filter("date")(now, "yyyy/MM/dd");
		$scope.viewModel.attendanceStratDate=new Date(monthStartDate);  //起止日期
		$scope.viewModel.attendanceEndDate=new Date(dayEndDate);	  	  //终止日期
		//日期控件
		$scope.viewModel.dateOptions = {
				formatYear: 'yy',
			    startingDay: 1,
			    showWeeks:false
		};
		//日期控件
		$scope.viewModel.format = "yyyy/MM/dd";
		//日期启动开关
		$scope.viewModel.startDateOpened = false;
		$scope.viewModel.endDateOpened = false;
		
		$scope.viewApprovalRecordModel={};
		$scope.viewApprovalRecordModel.dataList=[];
		
		// 设置选择日期45天
		var maxde = new Date();
		var minde = maxde.getTime()-1000*60*60*24*45;
		$scope.viewModel.minDate = new Date(minde);
		$scope.viewModel.maxDate = maxde;

		//查询页面名称
		MenuService.getMenuByUrl({url:'retroactive'},
		function(data){	
			$scope.viewModel.menuList=data;
		},function(error) {});
		
		//起止日期选择
		$scope.viewModel.startDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			 $scope.viewModel.startDateOpened = !$scope.viewModel.startDateOpened;
		};
		//起止日期选择
		$scope.viewModel.endDateOpen = function($event) {
			$event.preventDefault();
			$event.stopPropagation();
			 $scope.viewModel.endDateOpened = !$scope.viewModel.endDateOpened;
		};

		$scope.editModel = {};
		$scope.editModel.retroactive={};
		$scope.editModel.title="";
		
		//加载考勤状态数据
		$scope.viewModel.bindSelectData = function(){
			AttendanceService.getDictionaryListBydicCls({
				dicCls:"1"
			},
			function(data){
				$scope.viewModel.attendanceStateData = data;
			},function(error) {
						
			});
		};
		$scope.viewModel.bindSelectData();
		
		//加载数据
		$scope.viewModel.bindData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			Service.getRetroactiveNum({
				attendanceDateStart:$scope.viewModel.attendanceStratDate,
				attendanceDateEnd:$scope.viewModel.attendanceEndDate
			},
			function(data){
				$scope.viewModel.retroactiveNumber=data;
			},function(error) {
			});
		
			//获取基本信息
			Service.getRetroactiveList({
				pageNo:$scope.viewModel.rowCount,
				pageCount:$scope.viewModel.perPage,
				attendanceDateStart:$scope.viewModel.attendanceStratDate,
				attendanceDateEnd:$scope.viewModel.attendanceEndDate
			},
			function(data){
				$scope.viewModel.dataList = data.retroctiveBeanList;
				$scope.viewModel.totalData = data.pageTotal;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		$scope.viewModel.bindData();
		
		$scope.viewModel.bindRoleData = function(){
			$scope.viewModel.isLoading=true;
			//获取基本信息
			RoleService.getRoleListByEmployeeId({},
			function(data){
				$scope.viewModel.roleList = data;
				$scope.viewModel.isLoading=false;
			},function(error) {
				$scope.viewModel.isLoading=false;
			});
		};
		$scope.viewModel.bindRoleData();
		
		//加载
		$scope.viewModel.load=function(){
			$scope.viewModel.show=true;
			//小时控件jquery-timepicker
		    $("#rStartTime").datetimepicker({
				format: 'H:i',
				timepicker:true,
				datepicker:false,
				allowBlank:true,
				onSelectTime: function(dateText, inst) {
					$scope.editForm.rStartTime.$valid=true;
					$scope.editForm.rStartTime.$invalid=true;
					$scope.editModel.retroactive.startTime=$filter("date")(dateText, "HH:mm");
			    }
			});
		    $("#rEndTime").datetimepicker({
				format: 'H:i',
				timepicker:true,
				datepicker:false,
				allowBlank:true,
				onSelectTime: function(dateText, inst) {
					$scope.editForm.rEndTime.$valid=true;
					$scope.editForm.rEndTime.$invalid=true;
					$scope.editModel.retroactive.endTime=$filter("date")(dateText, "HH:mm");
			    }
			});
		};
		//关闭加载
		$scope.viewModel.unLoad=function(){
			$scope.viewModel.show=false;
			$scope.editModel.user={};
		};
		//加载
		$scope.viewModel.loadSelect=function(){
			$scope.viewModel.showSelect=true;
		};
		//关闭加载
		$scope.viewModel.unLoadSelect=function(){
			$scope.viewModel.showSelect=false;
			$scope.editModel.user={};
		};

		//补签
		$scope.viewModel.addUser = function(retroctive){
			$scope.editModel.title="("+retroctive.attendanceStateName+")申请补签";
			$scope.editModel.attendanceDate=$filter("date")(retroctive.attendanceDate, "yyyy/MM/dd");
			$scope.editModel.startTime=retroctive.startTime;
			$scope.editModel.endTime=retroctive.endTime;
			$scope.editModel.retroactive.attendanceId=retroctive.attendanceId;
			$scope.editModel.attendanceState=retroctive.attendanceState;
			$scope.editModel.retroactive.startTime="";
			$scope.editModel.retroactive.endTime="";
			$scope.editModel.retroactive.description="";
			//判断考勤状态:
			if(retroctive.attendanceState=="2"){//2 迟到 
				$("#rEndTime").val(retroctive.endTime);
				$scope.editModel.retroactive.endTime=retroctive.endTime;
			}
			else if(retroctive.attendanceState=="3"){ //3 早退
				$("#rStartTime").val(retroctive.startTime);
				$scope.editModel.retroactive.startTime=retroctive.startTime;
			}
			//验证不能为空
			$scope.editForm.rStartTime.$dirty=false;
			$scope.editForm.rEndTime.$dirty=false;
			$scope.viewModel.load();
		};
		
		//查看过程
		$scope.viewModel.selectUser = function(retroctive){
			Service.getApprovalRecordById({
				retroactiveId:retroctive.retroactiveId
			},
			function(data){
				$scope.viewApprovalRecordModel.dataList = data;
				$scope.viewModel.isLoading=false;
				$scope.editModel.title="查看审批过程";
				$scope.viewModel.loadSelect();
			},function(error) {
				$rootScope.alertError(error.fault.message);
				$scope.viewModel.isLoading=false;
			});
			
		};
		
		//验证
		$scope.viewModel.requiredForm= function() {
			var type=false;
			if (($scope.editModel.retroactive.startTime=="" || $scope.editModel.retroactive.startTime==null) && ($scope.editModel.retroactive.endTime=="" || $scope.editModel.retroactive.endTime==null)) {
				$rootScope.alertWarning("补签上班签到、末班签退至少填一个。");
		    	type=true;
		    }
            if(type){
            	return true;
            }
        	if($scope.editModel.retroactive.startTime==null){
        		$rootScope.alertWarning("未选择补签上班签到时间！");
        		return true;
        	}else if($scope.editModel.retroactive.endTime==null){
        		$rootScope.alertWarning("未选择补签末班签退时间！");
        		return true;
        	}
        	return false;
        };
        
		//保存
		$scope.viewModel.save = function(){
			if($scope.viewModel.requiredForm()){
				return;
			}
			$scope.viewModel.isLoading = true;
			Service.saveRetroactive($scope.editModel.retroactive,
				function(data){
					$scope.viewModel.unLoad();
					$scope.viewModel.bindData();
					$rootScope.alertInfo("补签申请提交成功。");
					$scope.viewModel.isLoading = false;
				},function(error) {
					$rootScope.alertError(error.fault.message);
					$scope.viewModel.isLoading = false;
				});
		};
		
		//验证
		$scope.viewModel.validForm= function(isValid) {
            if (!isValid) {
                return;
            }else{
            	if($scope.viewModel.attendanceStratDate>$scope.viewModel.attendanceEndDate){
            		$scope.form.attendanceEndDate.$invalid=true;
            	}else{
            		$scope.viewModel.rowCount=1;
                	$scope.viewModel.bindData();
                	$scope.form.attendanceEndDate.$invalid=false;
            	}
            }
        };
        
        //验证补签按钮是否可用
        $scope.viewModel.isDisabled= function(retroctive) {
        	//if(retroctive.isApprovaled==0 || retroctive.isApprovaled==3){
        	if(retroctive.isApprovaled==1){
        		return true;
        	}
        	if(retroctive.attendanceState!=1){
        		return false;
        	}
        	return true;
        };
        //验证补签按钮是否可用
        $scope.viewModel.isDisabledSelect= function(retroctive) {
        	if(retroctive.isApprovaled==2){
        		return false;
        	}
        	return true;
        };
        
        $scope.viewModel.selected=function(role){
        	if(role.isRole==1){
        		$scope.editModel.retroactive.roleId=role.roleId;
        		return true;
        	}
        	return false;
        }; 
} ]);